package com.java46.front.order.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.java46.admin.user.dao.UserDaoT;
import com.java46.admin.user.entity.UsersEntity;
import com.java46.front.base.action.BaseAction;
import com.java46.front.base.exception.BusinessException;
import com.java46.front.dessert.entity.DessertEntity;
import com.java46.front.order.entity.OrdersEntity;
import com.java46.front.order.service.OrderService;
import com.java46.front.user.dao.UserDao;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderAction extends BaseAction {
    Logger looger = LogManager.getLogger(OrderAction.class);

    private String orderJson;

    public String getOrderJson() {
        return orderJson;
    }

    public void setOrderJson(String orderJson) {
        this.orderJson = orderJson;
    }

    private String orderState;
    private String userId;
    private String payMethod;
    private String count;
    private String dessertId;

    public void setOrderState(String orderState) {
        this.orderState = orderState;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getPayMethod() {
        return payMethod;
    }
    public void setPayMethod(String payMethod) {
        this.payMethod = payMethod;
    }
    public String getCount() {
        return count;
    }
    public void setCount(String count) {
        this.count = count;
    }
    public String getDessertId() {
        return dessertId;
    }
    public void setDessertId(String dessertId) {
        this.dessertId = dessertId;
    }
    private OrderService orderService=new OrderService();
    private UserDao userDao=new UserDao();
    @Action(value = "addOrders")
    public String addOrders(){
        Logger logger = LogManager.getLogger(OrderAction.class);
        //获取当前时间
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=format.format(date);
        //  判断页面传递的订单信息是否为空

        //  将json字符串转换成list集合
        List<DessertEntity> jsonList = new ArrayList<DessertEntity>();
        //
        String str = orderJson.substring(1,orderJson.length()-1);
        String [] strArray = str.split(",");

        int openIdIndex = str.lastIndexOf(",");
        String openId = str.substring(openIdIndex).split(":")[1];

        //调用service中添加订单的方法
        UsersEntity usersEntity = userDao.queryUser(openId);
        orderService.addOrders(usersEntity.getUserId(),time);
        OrdersEntity orders = orderService.getOrders(userId, time);
        for(int i =0;i<strArray.length;i++){
            if(i==strArray.length-1){
                break;
            }
            String s = strArray[i];
            String [] typeIdAndDessertId= s.split(":");
            String dessertId = typeIdAndDessertId[0];
            String count = typeIdAndDessertId[1];
            //调用service中添加订单详情的方法
            orderService.addOrderInfo(orders.getOrderId(),count,dessertId);
//            logger.error("类型："+count+"--"+dessertId);
        }

        //根据用户ID和下单时间得到一个Orders对象

        if(orders==null){
            throw new BusinessException("商品订单的信息为空...");
        }

//        logger.error("result:++"+orderJson.substring(1,orderJson.length()-1));
        //返回结果
        setSuccesResult("添加成功了哦");
        return "json";
    }
}
